<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>Excel批量文本替换工具</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.full.min.js"></script>
    <script>
        if (typeof XLSX === 'undefined') {
            document.write('<script src="https://unpkg.com/xlsx/dist/xlsx.full.min.js"><\/script>');
        }
    </script>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 20px auto;
            padding: 20px;
        }
        .container {
            border: 1px solid #ccc;
            padding: 20px;
            border-radius: 5px;
        }
        .input-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
        }
        input[type="text"] {
            width: 200px;
            padding: 5px;
        }
        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background-color: #45a049;
        }
        #status {
            margin-top: 20px;
            padding: 10px;
            border-radius: 4px;
        }
        .success {
            background-color: #dff0d8;
            color: #3c763d;
        }
        .error {
            background-color: #f2dede;
            color: #a94442;
        }
        .checkbox-group {
            margin: 10px 0;
        }
        .checkbox-group label {
            display: inline-block;
            margin-right: 20px;
            cursor: pointer;
        }
        .checkbox-group input[type="checkbox"] {
            margin-right: 5px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>Excel批量文本替换工具</h2>
        
        <div class="input-group">
            <label for="files">选择Excel文件（可多选）：</label>
            <input type="file" id="files" accept=".xlsx,.xls" multiple>
        </div>

        <div class="input-group">
            <label for="searchText">查找文本：</label>
            <input type="text" id="searchText" value="111">
        </div>

        <div class="input-group">
            <label for="replaceText">替换为：</label>
            <input type="text" id="replaceText" value="哈哈哈">
        </div>

        <div class="checkbox-group">
            <label>
                <input type="checkbox" id="exactMatch">
                精确匹配（只替换完全相同的文本）
            </label>
            <label>
                <input type="checkbox" id="replaceAll" checked>
                全部替换（替换所有匹配项）
            </label>
        </div>

        <button onclick="processFiles()">开始替换</button>

        <div id="status"></div>
    </div>

    <script>
        window.onload = function() {
            if (typeof XLSX === 'undefined') {
                document.getElementById('status').innerHTML = '错误：Excel处理库加载失败，请检查网络连接';
                document.getElementById('status').className = 'error';
                return;
            }
        };

        async function processFiles() {
            const files = document.getElementById('files').files;
            const searchText = document.getElementById('searchText').value;
            const replaceText = document.getElementById('replaceText').value;
            const exactMatch = document.getElementById('exactMatch').checked;
            const replaceAll = document.getElementById('replaceAll').checked;
            const status = document.getElementById('status');

            if (files.length === 0) {
                status.innerHTML = '请选择Excel文件';
                status.className = 'error';
                return;
            }

            status.innerHTML = '处理中...';
            status.className = '';

            for (let file of files) {
                try {
                    if (!file.name.match(/\.(xlsx|xls)$/i)) {
                        throw new Error('不支持的文件格式，请选择.xlsx或.xls文件');
                    }

                    const data = await file.arrayBuffer();
                    const workbook = XLSX.read(data);
                    let replacementCount = 0;

                    workbook.SheetNames.forEach(sheetName => {
                        const worksheet = workbook.Sheets[sheetName];
                        
                        for (let cell in worksheet) {
                            if (cell[0] === '!') continue;
                            
                            if (worksheet[cell].v !== undefined) {
                                const cellValue = String(worksheet[cell].v);
                                
                                if (exactMatch) {
                                    if (cellValue === searchText) {
                                        worksheet[cell].v = replaceText;
                                        replacementCount++;
                                    }
                                } else {
                                    if (cellValue.includes(searchText)) {
                                        if (replaceAll) {
                                            worksheet[cell].v = cellValue.replace(new RegExp(searchText, 'g'), replaceText);
                                        } else {
                                            worksheet[cell].v = cellValue.replace(searchText, replaceText);
                                        }
                                        replacementCount++;
                                    }
                                }
                            }
                        }
                    });

                    const newFile = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });
                    
                    const blob = new Blob([newFile], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
                    const url = URL.createObjectURL(blob);
                    const a = document.createElement('a');
                    a.href = url;
                    a.download = 'modified_' + file.name;
                    document.body.appendChild(a);
                    a.click();
                    document.body.removeChild(a);
                    URL.revokeObjectURL(url);
                    
                    status.innerHTML += `<br>文件 ${file.name} 处理成功！共替换了 ${replacementCount} 处内容。`;
                } catch (error) {
                    status.innerHTML += `<br>处理文件 ${file.name} 时出错: ${error.message}`;
                    status.className = 'error';
                    continue;
                }
            }

            status.innerHTML += '<br>所有文件处理完成！';
            status.className = 'success';
        }
    </script>
</body>
</html> 